<template>
    <!-- 页头 -->
  <div class="header">
        <!-- 头部 -->
    <div class="zhuce">
      <div class="zhuce-text"><p>绿野旅行-户外我的生活</p></div>
        <div class="top-login" v-if="this.$store.state.islogin==0">          
          <el-button type="text" @click="change" class="d1">登录</el-button>
          <div class="colorbox">
                <el-dialog :visible.sync="dialogVisible"  width="400px" >
                    <div class="tit"> 用户登录</div>
                    <el-input type="text" label="用户名" placeholder="请输入用户名" v-model="uname" ref="input"  @blur.native.capture="checkUname" style="margin-bottom: 20px;" ></el-input>
                    <el-input  type="password"  label="密码" v-model="upwd"   placeholder="请输入密码" @blur.native.capture="checkUpwd"></el-input>
                    <button @click="checkForm" type="primary" size="large" style="margin-top: 20px;" class="btn1">快速登录 </button>
                    <div class="pull-right color-aaa" style="margin-top: 30px;">
                      <a href="javascript:;" class="color-aaa">忘记密码</a>
                      &nbsp;|&nbsp;
                      <router-link to="/register" class="color-aaa">注册</router-link>
                    </div>
                </el-dialog>
          </div>
          <span id="loginstatus">
                <router-link class="d2" to="/register">注册</router-link>
          </span>
        </div>
        <div v-else  class="uuname">
        欢迎：{{this.$store.state.user.uname}}     
		      <el-button  l-button type="info" @click="loginout">退出</el-button>  
      </div>
    </div>
    <div class="header-top">
        <div class="logo"><img src="../assets/house/logo1.png"></div>
        <div class="text"><p>绿野旅行</p></div>
        <div class="t1"><p>户外我的生活</p></div>
        <div class="t2"><p>（网页地址）</p></div>
        <div class="search"><strong></strong><input type="text" class="search-text1" @keyup.13="searchKey" v-model="keyword"><input type="button" class="search-text2" value="搜索" @click="searchKey"></div>
    </div>
        <!-- 导航 -->
    <div class="header-menu">
        <ul class="menu">
            <li class="item1"><router-link to="/" >首页</router-link></li>
            <li class="item1"><router-link to="/selected/''" >商品列表</router-link></li>
            <li class="item1"><router-link to="/team" >团建定制</router-link></li>
            <li class="item1"><router-link to="/strategy" >攻略</router-link></li>
        </ul>
    </div>
    </div>
</template>
<script>
  export default {
    data() {
      return {
        uname: '',
        upwd: '',
        dialogVisible: false,
        keyword:''
      };
    },
    methods: {
      checkUname(){
      // 验证用户名
      let uname = this.uname;
      let reg = /^(\w|[\u4e00-\u9fa5]){2,8}$/;
      if(reg.test(uname)){ //true  验证成功
        return true;
      }else{  //false  不符合格式要求
        return false;
      }
    },

    checkUpwd(){
      // 验证密码
      let reg = /^\d{6}$/;
      if(reg.test(this.upwd)){ //true  验证成功
        return true;
      }else{  //false  不符合格式要求
        return false;
      }
    },

    /** 验证表单 点击按钮后触发 */
    checkForm(){
      if(this.checkUname() && this.checkUpwd()){  // 所有组件验证成功
        // 发送注册请求，实现注册业务
        let url = '/login';
        let param = 
          `uname=${this.uname}&upwd=${this.upwd}`;
        this.axios.post(url, param).then(result=>{
          // console.log(this.uname,this.upwd);
          // console.log(result);
          if(result.data.code==200){ // 登录成功
            this.$message({
            message: '恭喜你，登录成功',
            type: 'success'
            })
            // 调用mutations更新登录状态
            let user = result.data.result;
            this.$store.commit('updateLoginState', user);

            // 把用户信息存入sessionStorage，刷新页面时用
            sessionStorage.setItem('islogin', 1);
            sessionStorage.setItem('user', JSON.stringify(user));            
            this.$router.push('/');
          }else { // 登录失败
            this.$message('账号密码输入错误')
          }
        })
      }else{ // 其中有格式错误的组件

      }
    },
    loginout(){
      sessionStorage.clear();
      this.$router.push('/');
      window.location.reload();
    },
    change(){
      this.dialogVisible=true;
      this.$nextTick(()=>{
      this.$refs.input.focus();
    })
  },
  searchKey(){
    if(this.keyword)
    this.$router.push(`/selected/${this.keyword}`);
  }
  },
}
</script>
<style scoped>
/* 页头样式 */
.zhuce{
  display: flex;
  height: 50px;
  background-color: #f9f7f6;
  padding: 0 0 0 10px;
}
.zhuce>.zhuce-text{
  margin-top: 5px;
  color: #666;
  width: 450px;
  height: 35px;
  line-height: 35px;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.top-login{
  margin-top: 15px;
  margin-left: 900px;
  color: #666;
  font-size: 12px;
}
.top-login>span>a{
    color: #409EFF; 
    position: relative;
    font-size: 14px;
    text-decoration: none;
    left: 80px;
    bottom: 27px;
}
.el-button--text {
    color: #409EFF;
    background: 0 0;
    padding-top: 0;
    padding-right: 0;
}
.uuname{position: absolute;right: 100px;padding: 5px;}
.header-top{
  display: flex;
  margin-left: 150px;
  margin-top: 60px;
}
.header-top>.logo{
  margin-top: -60px;
  margin-left: -150px;
  width: 298px;
  height: 85px;
}
.header-top>.text{
  margin-top: 15px;
  margin-left: 150px;
  font-size: 40px;
  font-style: normal;
  color: #31c27c;
  font-weight: bold;
  font-family: YADLvIVLCEI 0;
}
.header-top>.t1{
  margin-top: 20px;
  color: #31c27c;
  font-size: 20px;
}
.header-top>.t2{
  margin-left: -130px;
  margin-top: 40px;
  color: #31c27c;
  font-size: 20px;
}

.header-top>.search{
  float: left;
  margin-left: 300px;
  margin-top: 50px;
}
.header-top>.search>.search-text1{
  width: 270px;
  height: 34px;
  border:2px solid #ccc;
}
.header-top>.search>.search-text1:focus{border-color: #0aa1ed;}
.header-top>.search>.search-text2{
  float: right;
  color: #fff;
  width: 80px;
  height: 40px;
  line-height: 35px;
  text-align: center;
  font-size: 14px;
  cursor: pointer;
  background-color: #4dc21b;
  border: 0;
}

.header-menu{
  margin-top: 65px;
  color: white;
  height: 45px;
  line-height: 45px;
  padding: 0 20px;
  font-size: 16px;
  position: relative;
  background-image: url(../assets/house/ppg.jpg) ;
}
.header-menu>.menu{
  display: flex;

}
.header-menu>ul>li{
  list-style: none;
}
.header-menu>ul>li>a{
  display: block;
  color: white;
  height: 45px;
  line-height: 45px;
  padding: 0 20px;
  font-size: 16px;
  position: relative;
  text-decoration: none;
}

#colerbox{
  z-index: 1000;
}
.tit{
    font-size: 20px;
    text-align: center;
    padding: 20px 0 30px;
    
}
.btn1{
   background: #BDCEFC;
    border-radius: 25px;
    width: 300px;
    height: 50px;
    height: 50px;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    color: #fff;
    background-color: #31c272;
    border: 0;
    margin-left: 30px;
}
.pull-right {
    text-align: center;
}
.color-aaa {
    color: #aaa;
}
</style>